package com.qfedu.firstpage.controller;

import com.qfedu.firstpage.entity.Bbook;
import com.qfedu.firstpage.entity.Blist;
import com.qfedu.firstpage.entity.ResponseData;
import com.qfedu.firstpage.service.SearchBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ban
 * @date 2021/3/24 - 17:48
 */
@Api("firstPage") // 注解在类上，表明当前类需要生成 在线文档
@RestController
public class SearchBookController {


    @Autowired
    private SearchBookService searchBookService;

    /**
     * 模糊查询 根据 书名
     * @param state 0 是 书籍 ， 1 是 书单
     * @return
     */
    @ApiOperation(value = " 模糊查询 根据 书名",produces = "application/json")
    @RequestMapping("/findBookLikeBookName")
    public ResponseData findBookLikeBookName(@RequestParam("name") String name, @RequestParam("state") int state) {

        ResponseData responseData = new ResponseData();

        List<Bbook> likeBookName = searchBookService.findBookLikeBookName(name, state);
        responseData.setData(likeBookName);
        responseData.setCode("1");
        responseData.setMessage("success");
        return responseData;
    }

    /**
     * 用户 添加 书籍 到 书架中
     * @param bid
     * @param uid
     * @return
     */
    @RequestMapping("/insertBbookInBlist")
    public ResponseData insertBbookInBlist(@RequestParam("bid") int bid, @RequestParam("uid") int uid) {

     boolean flag =  searchBookService.insertBbookInBlist(bid, uid);
        ResponseData responseData = new ResponseData();
     if (flag) {
         responseData.setCode("1");
         responseData.setMessage("success");
     } else {
         responseData.setCode("0");
         responseData.setMessage("fail");
     }
     return responseData;
    }


    /**
     * 获取 当前用户书架的 所有 信息
     * @param uid
     * @return
     */
    @RequestMapping("/findBbookrackByUid")
    public ResponseData findBbookrackByUid(@RequestParam("uid") int uid) {


        ResponseData responseData = new ResponseData();

        List<Bbook> bbookrackByUid = searchBookService.findBbookrackByUid(uid);
        responseData.setData(bbookrackByUid);
        responseData.setCode("1");
        responseData.setMessage("success");
        return responseData;
    }


    /**
     *  删除 用户 指定书架的书籍
     * @param bids
     * @param uid
     * @return
     */
    @RequestMapping("/deleteBuserBbook")
    public ResponseData deleteBuserBbook(@RequestParam("bids") int[] bids, @RequestParam("uid") int uid) {
        boolean flag = searchBookService.deleteBuserBbook(bids, uid);
        ResponseData responseData = new ResponseData();
        if (flag) {
            responseData.setCode("1");
            responseData.setMessage("success");
        } else {
            responseData.setCode("0");
            responseData.setMessage("fail");
        }
        return responseData;
    }

    /**
     * 用户 点击 书籍 把书籍 加入 到 浏览历史中 如果 添加 过了 修改 浏览时间
     * @param uid
     * @param bid
     * @return
     */
    @RequestMapping("/insertbbookHistory")
    public ResponseData insertbbookHistory(@RequestParam("uid") int uid, @RequestParam("bid") int bid) {


        boolean flag = searchBookService.insertbbookHistory(uid, bid);
        ResponseData responseData = new ResponseData();
        if (flag) {
            responseData.setCode("1");
            responseData.setMessage("success");
        } else {
            responseData.setCode("0");
            responseData.setMessage("fail");
        }
        return responseData;

    }


    /**
     *
     * 在 当前 浏览历史中  已 name 来模糊查询
     *
     * 如果 name 为空 ，则查询 当前用户 所有的浏览历史
     *
     * @param uid
     * @param name
     * @return
     */
    @RequestMapping("/findbuserHistoryAllBbook")
    public ResponseData findbuserHistoryAllBbook(@RequestParam("uid") int uid, @Param("name") String name) {
        ResponseData responseData = new ResponseData();

        List<Bbook> bbookrackByUid = searchBookService.findbuserHistoryAllBbook(uid, name);
        responseData.setData(bbookrackByUid);
        responseData.setCode("1");
        responseData.setMessage("success");
        return responseData;
    }

    /**
     * 删除当前用户  浏览历史中 指定 bid 书籍 的信息
     * @param uid
     * @param bid
     * @return
     */
    @RequestMapping("/deleteUidHistoryBook")
    public ResponseData deleteUidHistoryBook(@RequestParam("uid")int uid, @RequestParam("bid") int bid) {

     boolean flag = searchBookService.deleteUidHistoryBook(uid, bid);
        ResponseData responseData = new ResponseData();
        if (flag) {
            responseData.setCode("1");
            responseData.setMessage("success");
        } else {
            responseData.setCode("0");
            responseData.setMessage("fail");
        }
        return responseData;
    }

    /**
     *  查询 当前 用户 所有的 书单列表
     * @param uid
     * @return
     */
    @RequestMapping("/findAllUidBlist")
    public ResponseData findAllUidBlist(@RequestParam("uid")int uid) {

        ResponseData responseData = new ResponseData();

        List<Blist> allUidBlist = searchBookService.findAllUidBlist(uid);
        responseData.setData(allUidBlist);
        responseData.setCode("1");
        responseData.setMessage("success");
        return responseData;
    }

    /**
     * 查询 当前 书单的 所有 书籍 信息
     * @param lid
     *
     * @return
     */
    @RequestMapping("/findblistBylid")
    public ResponseData findblistBylid(@RequestParam("lid") int lid) {

        ResponseData responseData = new ResponseData();

        List<Bbook> bbooks = searchBookService.findblistBylid(lid);
        responseData.setData(bbooks);
        responseData.setCode("1");
        responseData.setMessage("success");
        return responseData;
    }


    /**
     * 把 用户 浏览过得 书单 加入 到 书单浏览历史中 如果 添加 过了 修改 浏览时间
     * @param uid
     * @param lid
     * @return
     */
    @RequestMapping("/addlistInhistory")
    public ResponseData addlistInhistory(@RequestParam("uid")int uid, @RequestParam("lid") int lid) {

      boolean flag = searchBookService.addlistInhistory(uid, lid);

        ResponseData responseData = new ResponseData();
        if (flag) {
            responseData.setCode("1");
            responseData.setMessage("success");
        } else {
            responseData.setCode("0");
            responseData.setMessage("fail");
        }
        return responseData;
    }

    /**
     * 用户 删除 自己的 书单浏览历史
     * @param uid
     * @param lid
     * @return
     */
    @RequestMapping("/deletelistInhistory")
    public ResponseData deletelistInhistory(@RequestParam("uid")int uid, @RequestParam("lid") int lid) {

        boolean flag = searchBookService.deletelistInhistory(uid, lid);
        ResponseData responseData = new ResponseData();
        if (flag) {
            responseData.setCode("1");
            responseData.setMessage("success");
        } else {
            responseData.setCode("0");
            responseData.setMessage("fail");
        }
        return responseData;
    }
}
